package com.cwt_taxi_xa.protocol.can;

import android.os.Handler;
import android.os.Message;
import android.util.Log;

import com.cwt_taxi_xa.protocol.MCUUtil;
import com.cwt_taxi_xa.service.SerialPortService;
import com.cwt_taxi_xa.util.AppLog;
import com.cwt_taxi_xa.util.Tools;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by undi on 2019/6/1 17:28
 */
public class ConditioningDecode {

	final static String TAG = "ConditioningDecode";
	//空调
	public static final String CAN_ID = "3B010000";
	//空调燃油加热器燃油不足
	public static final String CAN_HEATER_FUEL_ID = "DB020000";
	//新增 PTC/燃油加热器切换按键
	public static final String CAN_HEATER_TYPE_ID = "33040000";

	//除去canId部分
	private final int dataIndex = 3;
	private final int errorCodeWhat = 1;
	private final int temperatureWhat = 2;
	private final int temperatureUnitWhat = 3;
	private final int levelWhat = 4;
	private final int outletModeWhat = 5;
	private final int powerWhat = 6;
	private final int fuelAlarmWhat = 7;
	private final int heaterTypeWhat = 8;
	private final int heatingModeWhat = 9;

	private int power = 0;
	//温度
	private String temperature ="26";
	//温度单位
	private String temperatureUnit = "℃";
	//风量
	private int windLevel = 5;
	//出风模式
	private int outletMode = 0;
	//错误码状态
	private int errorCode = 0;
	//空调燃油加热器警告
	private int fuelAlarm = 0;
	//空调加热模式标志位
	private int heaterType = 0;
	//加热器模式
	private int heatingMode = 0;
	private boolean fuelAlarmInit = false;

	WeakReference<IViewWindowManager> weakReference;

	List<WeakReference<IViewAirConditionState>> weakReferenceStateArray = new ArrayList<>();

	List<WeakReference<IViewAirCondition>> weakReferenceArray = new ArrayList<>();

	private Handler mHandler = new Handler(){

		@Override
		public void handleMessage(Message msg) {
			switch(msg.what){
				case errorCodeWhat:
					dispatchError((int)msg.obj);
					break;
				case levelWhat:
					dispatchLevel((int)msg.obj);
					break;
				case outletModeWhat:
					dispatchOutletMode((int)msg.obj);
					break;
				case temperatureWhat:
					dispatchTemp(((String)msg.obj));
					break;
				case temperatureUnitWhat:
					dispatchTempUnit(((String)msg.obj));
					break;
				case powerWhat:
					dispatchPower(((int)msg.obj));
					break;
				case heatingModeWhat:
					dispatchHeatingMode((int)msg.obj);
					break;
				case fuelAlarmWhat:
					dispatchFuelAlarm((int)msg.obj);
					break;
				case heaterTypeWhat:
					dispatchHeaterType((int)msg.obj);
					break;
				default:
					break;
			}
			//mHandler.sendMessageDelayed(msg, 1000);
			//mHandler.sendMessage(mHandler.obtainMessage(what, obj))
		}

	};

	public void init(){
		//用于创建handler,避免在Thread中创建
	}

	public int getFuelAlarm() {
		return fuelAlarm;
	}

	public int getHeatingMode() {
		return heatingMode;
	}

	public int getPower() {
		return power;
	}


	public void register(IViewAirCondition viewAirCondition){
		if(!contains(viewAirCondition)){
			init(viewAirCondition);
			weakReferenceArray.add(new WeakReference<>(viewAirCondition));
		}
	}

	private void init(IViewAirCondition viewAirCondition){
		viewAirCondition.windLevel(windLevel);
		viewAirCondition.outletMode(outletMode);
		viewAirCondition.showTemperature(temperature);
		viewAirCondition.showTemperatureUnit(temperatureUnit);
		viewAirCondition.power(power);
		viewAirCondition.heatingMode(heatingMode);
		viewAirCondition.heaterType(heaterType);

		viewAirCondition.errorCode(errorCode);

		//dispatchError(errorCode);
		//dispatchLevel(windLevel);
		//dispatchOutletMode(outletMode);
		//dispatchTemp(temperature);
		//dispatchTempUnit(temperatureUnit);
		//dispatchPower(power);
		//dispatchHeatingMode(heatingMode);
		//dispatchHeaterType(heaterType);
	}


	public boolean contains(IViewAirCondition viewAirCondition){
		boolean result = false;
		synchronized(weakReferenceArray){
			for(int i = 0; i < weakReferenceArray.size(); i++){
				if(viewAirCondition == weakReferenceArray.get(i).get()){
					result = true;
					break;
				}
			}
		}
		return result;
	}

	public void unregister(IViewAirCondition viewAirCondition){
		synchronized(weakReferenceArray){
			for(int i = 0; i < weakReferenceArray.size(); i++){
				if(viewAirCondition == weakReferenceArray.get(i).get()){
					weakReferenceArray.remove(i);
					break;
				}
			}
		}
	}

	public void register(IViewWindowManager viewWindowManger){
		weakReference  = new WeakReference<>(viewWindowManger);
	}

	public void registerState(IViewAirConditionState viewAirConditionState){
		if(!contains(viewAirConditionState)){
			init(viewAirConditionState);
			weakReferenceStateArray.add(new WeakReference<>(viewAirConditionState));
		}
	}

	private void init(IViewAirConditionState viewAirConditionState){
		if(viewAirConditionState != null){
			viewAirConditionState.fuelAlarm(fuelAlarm);
			viewAirConditionState.power(power);
			viewAirConditionState.heatingMode(heatingMode);
			viewAirConditionState.heaterType(heaterType);
		}
	}


	public boolean contains(IViewAirConditionState viewAirConditionState){
		boolean result = false;
		synchronized(weakReferenceStateArray){
			for(int i = 0; i < weakReferenceStateArray.size(); i++){
				if(viewAirConditionState == weakReferenceStateArray.get(i).get()){
					result = true;
					break;
				}
			}
		}
		return result;
	}




	void dispatchError(int code){
		synchronized(weakReferenceArray){
			for(int i =0 ;i< weakReferenceArray.size(); i++){
				IViewAirCondition viewAirCondition = weakReferenceArray.get(i).get();
				if(viewAirCondition != null){
					viewAirCondition.errorCode(code);
				}else{
					weakReferenceArray.remove(i);
				}
			}
		}
	}

	void dispatchPower(int type){
		synchronized(weakReferenceArray){
			for(int i =0 ;i< weakReferenceArray.size(); i++){
				IViewAirCondition viewAirCondition = weakReferenceArray.get(i).get();
				if(viewAirCondition != null){
					viewAirCondition.power(type);
				}else{
					weakReferenceArray.remove(i);
				}
			}
		}

		if(weakReference != null){
			IViewWindowManager viewWindowManger  = weakReference.get();
			if(viewWindowManger != null){
				viewWindowManger.power(type);
			}
		}

		synchronized(weakReferenceStateArray){
			for(int i =0 ;i< weakReferenceStateArray.size(); i++){
				IViewAirConditionState viewAirConditionState = weakReferenceStateArray.get(i).get();
				if(viewAirConditionState != null){
					viewAirConditionState.power(type);
				}else{
					weakReferenceStateArray.remove(i);
				}
			}
		}
	}

	void dispatchFuelAlarm(int fuelAlarm){
		if(weakReference != null && !fuelAlarmInit){
			IViewWindowManager viewWindowManger  = weakReference.get();
			if(viewWindowManger != null){
				viewWindowManger.fuelAlarm(fuelAlarm);
			}
		}

		synchronized(weakReferenceStateArray){
			for(int i =0 ;i< weakReferenceStateArray.size(); i++){
				IViewAirConditionState viewAirConditionState = weakReferenceStateArray.get(i).get();
				if(viewAirConditionState != null){
					viewAirConditionState.fuelAlarm(fuelAlarm);
				}else{
					weakReferenceStateArray.remove(i);
				}
			}
		}
	}


	void dispatchHeaterType(int heaterType){
		synchronized(weakReferenceArray){
			for(int i =0 ;i< weakReferenceArray.size(); i++){
				IViewAirCondition viewAirCondition = weakReferenceArray.get(i).get();
				if(viewAirCondition != null){
					viewAirCondition.heaterType(heaterType);
					AppLog.d(TAG,"dispatchHeaterType heaterType:" + i +  " heaterType:" + heaterType);
				}else{
					weakReferenceArray.remove(i);
				}
			}
		}

		synchronized(weakReferenceStateArray){
			for(int i =0 ;i< weakReferenceStateArray.size(); i++){
				IViewAirConditionState viewAirConditionState = weakReferenceStateArray.get(i).get();
				if(viewAirConditionState != null){
					viewAirConditionState.heaterType(heaterType);
				}else{
					weakReferenceStateArray.remove(i);
				}
			}
		}
	}

	void dispatchHeatingMode(int heatingMode){
		synchronized(weakReferenceArray){
			synchronized(weakReferenceArray){
				for(int i =0 ;i< weakReferenceArray.size(); i++){
					IViewAirCondition viewAirCondition = weakReferenceArray.get(i).get();
					if(viewAirCondition != null){
						viewAirCondition.heatingMode(heatingMode);
						AppLog.d(TAG,"dispatchHeatingMode heaterType:" + i +  " heatingMode:" + heatingMode);
					}else{
						weakReferenceArray.remove(i);
					}
				}
			}
		}

		synchronized(weakReferenceStateArray){
			for(int i =0 ;i< weakReferenceStateArray.size(); i++){
				IViewAirConditionState viewAirConditionState = weakReferenceStateArray.get(i).get();
				if(viewAirConditionState != null){
					viewAirConditionState.heatingMode(heatingMode);
				}else{
					weakReferenceStateArray.remove(i);
				}
			}
		}
	}

	void dispatchLevel(int level){
		synchronized(weakReferenceArray){
			for(int i =0 ;i< weakReferenceArray.size(); i++){
				IViewAirCondition viewAirCondition = weakReferenceArray.get(i).get();
				AppLog.d(TAG,"dispatchLevel viewAirCondition:" + (viewAirCondition == null? "null":"viewAirCondition.windLevel"));
				if(viewAirCondition != null){
					viewAirCondition.windLevel(level);
					AppLog.d(TAG,"dispatchLevel index:" + i +  " viewAirCondition:" + (viewAirCondition == null? "null":"viewAirCondition.windLevel"));
				}else{
					weakReferenceArray.remove(i);
				}
			}
		}

	}

	void dispatchOutletMode(int type){
		synchronized(weakReferenceArray){
			for(int i =0 ;i< weakReferenceArray.size(); i++){
				IViewAirCondition viewAirCondition = weakReferenceArray.get(i).get();
				if(viewAirCondition != null){
					viewAirCondition.outletMode(type);
				}else{
					weakReferenceArray.remove(i);
				}
			}
		}

	}

	void dispatchTemp(String temp){
		synchronized(weakReferenceArray){
			for(int i =0 ;i< weakReferenceArray.size(); i++){
				IViewAirCondition viewAirCondition = weakReferenceArray.get(i).get();
				if(viewAirCondition != null){
					viewAirCondition.showTemperature(temp);
				}else{
					weakReferenceArray.remove(i);
				}
			}
		}

	}

	void dispatchTempUnit(String temp){
		synchronized(weakReferenceArray){
			for(int i =0 ;i< weakReferenceArray.size(); i++){
				IViewAirCondition viewAirCondition = weakReferenceArray.get(i).get();
				if(viewAirCondition != null){
					viewAirCondition.showTemperatureUnit(temp);
				}else{
					weakReferenceArray.remove(i);
				}
			}
		}

	}


	public void setPower(int power) {
		if(this.power != power){
			mHandler.sendMessage(mHandler.obtainMessage(powerWhat,power));
		}
		this.power = power;
	}

	public void setTemperature(String temperature) {
		if(!this.temperature.equals(temperature)){
			mHandler.sendMessage(mHandler.obtainMessage(temperatureWhat,temperature));
		}
		this.temperature = temperature;

	}

	public void setTemperatureUnit(String temperatureUnit) {
		if(!this.temperatureUnit.equals(temperatureUnit)){
			//dispatchTemp4C(temperature);
			mHandler.sendMessage(mHandler.obtainMessage(temperatureUnitWhat,temperatureUnit));
		}
		this.temperatureUnit = temperatureUnit;
	}

	public void setWindLevel(int windLevel) {
		if(this.windLevel != windLevel){
			mHandler.sendMessage(mHandler.obtainMessage(levelWhat,windLevel));
		}
		this.windLevel = windLevel;
	}

	public void setOutletMode(int outletMode) {
		if(this.outletMode != outletMode){
			mHandler.sendMessage(mHandler.obtainMessage(outletModeWhat, outletMode));
		}
		this.outletMode = outletMode;
	}

	public void setErrorCode(int errorCode) {
		if(this.errorCode != errorCode ){
			mHandler.sendMessage(mHandler.obtainMessage(errorCodeWhat, errorCode));
		}
		this.errorCode = errorCode;
	}

	public void setHeatingMode(int heatingMode) {
		if(this.heatingMode != heatingMode){
			mHandler.sendMessage(mHandler.obtainMessage(heatingModeWhat, heatingMode));
		}
		this.heatingMode = heatingMode;
	}

	public void setFuelAlarm(int fuelAlarm) {
		if(this.fuelAlarm != fuelAlarm){
			mHandler.sendMessage(mHandler.obtainMessage(fuelAlarmWhat, fuelAlarm));
		}
		this.fuelAlarm = fuelAlarm;
	}

	public void setHeaterType(int heaterType) {
		if(this.heaterType != heaterType){
			mHandler.sendMessage(mHandler.obtainMessage(heaterTypeWhat, heaterType));
		}
		this.heaterType = heaterType;
	}

	public boolean isFuelAlarmInit() {
		return fuelAlarmInit;
	}

	public void setFuelAlarmInit(boolean fuelAlarmInit) {
		this.fuelAlarmInit = fuelAlarmInit;
	}

	/*
	 *
	 */
	public void decodeHeaterFuel(byte[] buffer){
		byte data = buffer[dataIndex + 7];
		Log.d(TAG,"decodeHeaterFuel data:" + data);
		//00 00 02 DB
		//00 00 00 00 00 00 40 00
		//0x00：无效
		//0x01：油量不足（报警信号）
		//0x02：油量充足（非报警信号）
		//0x03：预留
		int fuelAlarm = (data>>2)&0x03;
		//00 00 00 00 00 00 0C 00
		if(fuelAlarm == 2){
			//08
			Log.d(TAG," decodeHeaterFuel  油量充足");
		}else if(fuelAlarm == 1){
			//04
			//并发出“滴”一声报警，5s
			//后该提示框自动收回，每个上电周期内，该提示文字仅显示 1 次，导航屏收到其他动作信号或状态报
			//文，导航屏不弹框。显示位置可根据已有界面选择合适位置，但需醒目，易于驾驶员看到
			Log.d(TAG," decodeHeaterFuel 空调燃油加热器燃油不足,请及时添加");
		}else if(fuelAlarm == 0){
			//00
			Log.d(TAG," decodeHeaterFuel  无效");
		}else{
			//0C
			Log.d(TAG," decodeHeaterFuel  预留");
		}
		setFuelAlarm(fuelAlarm);
		//0x00：无效
		//0x01：PTC 加热模式
		//0x02：燃油加热器模式
		//0x03：预留
		int heatingMode = (data>>4)&0x03;
		if(2 == heatingMode){
			//20
			Log.d(TAG,"heatingMode 燃油加热器模式");
		}else if(1 == heatingMode){
			//10
			Log.d(TAG,"heatingMode PTC 加热模式");
		}else if(0 == heatingMode){
			//00
			Log.d(TAG,"heatingMode 无效");
		}else{
			//30
			Log.d(TAG,"heatingMode 预留:" + heatingMode);
		}
		setHeatingMode(heatingMode);
		Log.d(TAG,"fuelAlarm:" + fuelAlarm + " heatingMode:" + heatingMode);
	}

	/*
	 *
	 */
	public void decodeHeaterType(byte[] buffer){
		//00 00 04 33
		//00 00 00 07 00 00 00 00
		byte data = buffer[dataIndex + 4];
		//Log.d(TAG,"decodeHeaterType data:" + data);
		//0x00：无效
		//0x01：PTC 加热
		//0x02：燃油加热器
		//0x03：PTC+燃油加热器
		//0x04~0x07：预留
		int heatingMode = data&0x07;
		if(3 == heatingMode){
			Log.d(TAG,"decodeHeaterType PTC+燃油加热器");
		}else if(2 == heatingMode){
			Log.d(TAG,"decodeHeaterType 燃油加热器");
		}else if(1 == heatingMode){
			Log.d(TAG,"decodeHeaterType PTC 加热");
		}else if(0 == heatingMode){
			Log.d(TAG,"decodeHeaterType 无效");
		}else{
			Log.d(TAG,"decodeHeaterType 预留:" + heatingMode);
		}
		setHeaterType(heatingMode);
	}

	/*
	 * 空调1帧数据
	 */
	public void decode(byte[] buffer){
		//String id = getCanId(buffer);
		//byte[] dataBuffer = buffer;
		Log.d(TAG,"buffer:" + Tools.parseByte2HexStr(buffer));
		//1.1.1 温度显示
		errorCode(buffer[dataIndex + 8],buffer[dataIndex + 5]);

		unitConversion(buffer[dataIndex + 4],buffer[dataIndex + 5],buffer[dataIndex + 8],buffer[dataIndex + 2]);

		//1.1.2 出风模式
		outletMode(buffer[dataIndex + 3]&0x0F);
		//1.1.3 "风量档位"的显示
		windLevel(buffer[dataIndex + 2]&0x03,buffer[dataIndex + 3]);
	}

	private String getCanId(byte[] buffer){
		return "";
	}

	private byte[] getCanData(byte[] buffer){
		return new byte[]{};
	}
	//数值索引从0开始,当前索引1

	/**
	 * 空调开关
	 * 字节位 2.0~2.1 长度:2
	 * @param power
	 */
	@Deprecated
	private byte power(byte power){
		power &= 0x03;
		if(power == 0x00){
			//0x0:无效;
			Log.d(TAG,"空调开关:无效");
			//多功能屏显示 保持上一状态。
		}else if(power == 0x01){
			//0x1:打开;
			Log.d(TAG,"空调开关:打开");
		}else if(power == 0x02){
			//0x2:关闭;
			Log.d(TAG,"空调开关:关闭");

			//不显示温度值、单位℃（℉）及“温度”
			//连续5秒没有采集到0x13B的报文时，不显示温度值、单位℃（℉）及“温度”
		}else if(power == 0x03){
			//0x3:保留;
			Log.d(TAG,"空调开关:保留");
			//多功能屏显示 保持上一状态。
		}
		return power;
	}
	//数值索引从0开始,当前索引3
	/**
	 * 温度单位切换
	 * @param type 字节位 4.7 长度:1
	 *    //0x00：摄氏设定温度
	 *    //0x01：华氏设定温度
	 * @param value 字节位 5.0~5.7 长度:8
	 *    //0x00~0x10:无效
	 *    //0x11：显示 Lo
	 *    //0x12~20:设定值
	 *    //0x21：显示 Hi
	 *    //0x22~0xff:无效
	 *
	 *    //0x00~0x3f:无效
	 *    //0x40：显示 Lo
	 *    //0x41~5a:设定值
	 *    //0x5b：显示 Hi
	 *    //0x5c~0xff:无效
	 * @param value 字节位 8.0~8.1 长度:2
	 *    //0x00:无效
	 *    //0x01:不显示
	 *    //0x02:显示
	 *    //0x03:保留
	 * @param power 字节位 2.0~2.1 长度:2
	 *    //0x00：无效
	 *    //0x01：打开
	 *    //0x02：关闭
	 *    //0x03：保留
	 */
	private void unitConversion(byte type,byte value,byte errorCode,byte power){
		power &= 0x03;
        errorCode &= 0x03;
		Log.d(TAG,"unitConversion type:" + type + " value:" + value + " errorCode:" + errorCode + " power:" + power );
		//故障码不显示
		if(errorCode == 0x01 && power == 0x01){
			setPower((int)power);
			int unitType = (type>>7)&0x01;//0x0:摄氏设定温度-0x1:华氏设定温度
			if(unitType == 0x00){
				setTemperatureUnit("℃");
				if(value == 0x11){
					Log.d(TAG,"温度℃:Lo");
					//mHandler.sendMessage(mHandler.obtainMessage(temperature4CWhat,));
					setTemperature("Lo");
				}else if(value == 0x21){
					Log.d(TAG,"温度℃:Hi");
					setTemperature("Hi");
					//mHandler.sendMessage(mHandler.obtainMessage(temperature4CWhat,"Hi"));
				}else if(value > 0x11 && value < 0x21){
					Log.d(TAG,"温度℃:" + value);
					//mHandler.sendMessage(mHandler.obtainMessage(temperature4CWhat,value + ""));
					setTemperature(String.valueOf(value));
				}else{
					Log.d(TAG,"温度℃:无效 " + value);
				}
			}else{
				setTemperatureUnit("℉");
				if(value == 0x40){
					Log.d(TAG,"温度℉:Lo");
					setTemperature("Lo");
					//mHandler.sendMessage(mHandler.obtainMessage(temperature4FWhat,"Lo"));
				}else if(value == 0x5B){
					Log.d(TAG,"温度℉:Lo");
					setTemperature("Hi");
					//mHandler.sendMessage(mHandler.obtainMessage(temperature4FWhat,"Hi"));
				}else if(value >0x40 && value < 0x5b){
					Log.d(TAG,"温度℉:" + value);
					setTemperature(String.valueOf(value));
					//mHandler.sendMessage(mHandler.obtainMessage(temperature4FWhat,value + ""));
				}else{
					Log.d(TAG,"温度℉:Lo");
				}
			}
		}else  if(power == 0x00 || power == 0x03){
			//0x0:无效;
			Log.d(TAG,"空调开关:无效||保留");
			setPower((int)power);
			//多功能屏显示 保持上一状态。
		}else if(power == 0x02){
			//0x2:关闭;
			Log.d(TAG,"空调开关:关闭");
			setPower((int)power);
			//不显示温度值、单位℃（℉）及“温度”
			//连续5秒没有采集到0x13B的报文时，不显示温度值、单位℃（℉）及“温度”
		}else{
			Log.d(TAG,"unitConversion 错误码:显示" + " errorCode:" + errorCode);
		}

		//value =;
	}

	//数值索引从0开始,当前索引4
	//故障码模式,数值索引从0开始,当前索引7

	/**
	 *  故障码模式
	 * @param value 字节位 8.0~8.1 长度:2
	 *    //0x00:无效
	 *    //0x01:不显示
	 *    //0x02:显示
	 *    //0x03:保留
	 */
	private void errorCode(byte value,byte errorCode){
		value &= 0x03;
		if(value == 0x00){
			Log.d(TAG,"错误码:无效");
			setErrorCode(value);
			//mHandler.sendMessage(mHandler.obtainMessage(errorCodeWhat, Integer.toHexString(errorCode & 0xFF)));
			//多功能屏不显示
		}else if(value == 0x01){
			//Log.d(TAG,"错误码:不显示");//不用处理
            setErrorCode(value);
		}else if(value == 0x02){
			Log.d(TAG,"错误码:显示" + " errorCode:" + errorCode);
			setErrorCode(value);
			setTemperature(Integer.toHexString(errorCode));
			//mHandler.sendMessage(mHandler.obtainMessage(errorCodeWhat, Integer.toHexString(errorCode & 0xFF)));
			//不显示温度值、单位℃（℉）和“温度(TEMP),故障码显示范围:00~99，超出范围后不显示
		}else if(value == 0x03){
			Log.d(TAG,"错误码:保留");
			setErrorCode(value);
			//mHandler.sendMessage(mHandler.obtainMessage(errorCodeWhat, Integer.toHexString(errorCode & 0xFF)));
			//多功能屏不显示
		}
	}

	/**
	 *
	 * @param value 字节位 3.0~3.3 长度:4
	 *    //0x00:无效
	 *    //0x01:吹面
	 *    //0x02:吹面+吹脚
	 *    //0x03:吹脚
	 *    //0x04:吹脚+除霜
	 *    //0x05:除霜
	 *    //0x06～0x07:保留
	 */
	private void outletMode(int value){
		Log.d(TAG,"outletMode value:" + value);
		if(value == 0x00){
			Log.d(TAG,"出风模式:无效");
		}else if(value == 0x01){
			Log.d(TAG,"出风模式:吹面");
		}else if(value == 0x02){
			Log.d(TAG,"出风模式:吹面+吹脚");
		}else if(value == 0x03){
			Log.d(TAG,"出风模式:吹脚");
		}else if(value == 0x04){
			Log.d(TAG,"出风模式:吹脚+除霜");
		}else if(value == 0x05){
			Log.d(TAG,"出风模式:除霜");//除霜标识可不显
		}else if(value >= 0x06){
			Log.d(TAG,"出风模式:保留");
		}
		setOutletMode(value);

	}

	/**
	 *
	 * @param value 字节位 3.4~3.7 长度:4
	 *    //0x00:不出风
	 *    //0x01:level 1
	 *    //0x2:level 2
	 *    //0x3:level 3
	 *    //0x4:level 4
	 *    //0x5:level 5
	 *    //0x6:level 6
	 *    //0x7:level 7
	 *    //0x8～0xf:保留
	 */
	private void windLevel(int power,byte value){
		Log.d(TAG,"windLevel power:" + power + " value:" + value);
		switch(power){
			case 0x01:
				//空调开关:打开
				int level = (value>>4)&0x0F;
				if(level == 0x00){
					Log.d(TAG,"出风:不出风");
					//采集到“空调出风档位”为“关”或者“预留”时“风量档位”界面丌显示
					//连续5s未采集到ID: 0x13B时，界面不显示
					setWindLevel(level);
					//mHandler.sendMessage(mHandler.obtainMessage(levelWhat,level));
				}else{
					Log.d(TAG,"出风:" + level);
					setWindLevel(level);
					//mHandler.sendMessage(mHandler.obtainMessage(levelWhat,level));
				}
				break;
			default:
				//“无效”或“关闭”或“预留”时，空调风量档位丌显示。
				break;
		}
	}
	//3帧
	private static void sendSerialData7(final byte[] buffer){
		new Thread(new Runnable() {
			@Override
			public void run() {
				int i = 0;
				while (i < 3){
					try {
						SerialPortService.getInstance().sendSerialData7(buffer);
						Thread.sleep(66);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					i++;
				}
			}
		}).start();
	}

	/**
	 * PTC 加热模式
	 */
	public static void setPtcHeater(){
		sendSerialData7(MCUUtil.getMCUData(Tools.parseHexStr2Byte("55 6B 000F 01 FC 04 00 00 FF FF FF FF 04 FF FF FF")));
	}
	/**
	 * 燃油加热器模式
	 */
	public static void setFuelHeater(){
		//000F 长度=数据的个数+2
		sendSerialData7(MCUUtil.getMCUData(Tools.parseHexStr2Byte("55 6B 000F 01 FC 04 00 00 FF FF FF FF 08 FF FF FF")));
	}
}
